package com.dexilaonian.adpter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.bean.NurDetailtBean;
import com.dexilaonian.bean.YiyangtBean;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;

import java.util.List;

public class NurDeatialAdapter extends RecyclerView.Adapter<NurDeatialAdapter.ViewHolder>{
    private final Context context;
    private final List<NurDetailtBean> mList_project;
    private MyItemClickListener mItemClickListener;


    public NurDeatialAdapter(Context context, List<NurDetailtBean> list_project) {
        this.context = context;
        mList_project = list_project;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.recyclerview_item, null);
        //将全局的监听传递给holder
        ViewHolder holder = new ViewHolder(view, mItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String name = mList_project.get(position).getName();
        String pic_url = mList_project.get(position).getPic_url();
        String introduce = mList_project.get(position).getIntroduce();
        String price = mList_project.get(position).getPrice();
        String content_time = String.valueOf(mList_project.get(position).getContent_time());
        if (!TextUtils.isEmpty(name)) {
            holder.tvItem.setText(name);
        }
        if (!TextUtils.isEmpty(introduce)) {
            holder.tvMin.setText(introduce);
        }
        if (!TextUtils.isEmpty(pic_url)) {
            String pro_url = Constant.base_pic_url + pic_url;
            ImageLoader.display2(context, pro_url, holder.ivRecycler);
        }
        if (!TextUtils.isEmpty(price)) {
            holder.project_money.setText("￥"+price);
        }
        if (!TextUtils.isEmpty(content_time)) {
            holder.project_time.setText(content_time + "分钟");
        }
    }

    @Override
    public int getItemCount() {
        if (mList_project.size() == 0) {
            return 0;
        } else {
            return mList_project.size();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        MyItemClickListener mListener;
        TextView tvItem;
        TextView tvMin;
        TextView project_money;
        TextView project_time;
        ImageView ivRecycler;

        public ViewHolder(View itemView, MyItemClickListener myItemClickListener) {
            super(itemView);
            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
            tvItem = (TextView) itemView.findViewById(R.id.tv_title_kan);
            tvMin = (TextView) itemView.findViewById(R.id.tv_min_kan);
            project_money = (TextView) itemView.findViewById(R.id.tv_project_money);
            project_time = (TextView) itemView.findViewById(R.id.tv_project_time);
            ivRecycler = (ImageView) itemView.findViewById(R.id.iv_recycler);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v, getPosition());
            }

        }
    }
    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
